﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace CommonLibrary.CustControls
{
    public partial class FrmAddCols : Form
    {
        private static String prifixType = "System.Windows.Forms.";
        private FrmEditCols frmEditCols;

        public FrmAddCols(FrmEditCols frmEditCols)
        {
            InitializeComponent();
            this.frmEditCols = frmEditCols;
            this.TxtColName.Text = "Column1";
            this.TxtColTitile.Text = "Column1";
            this.CbmColType.Text = this.CbmColType.Items[5].ToString();
            this.CboFillable.Text = this.CboFillable.Items[0].ToString();
            this.CboGather.Text = this.CboGather.Items[0].ToString();
        }

        public void BtnConfirm_Click(object sender, EventArgs e)
        {
            if (!this.ValidateColName())
                return;
            String colName = this.TxtColName.Text.Trim();
            String colType = prifixType + this.CbmColType.Text;
            String colTitle = this.TxtColTitile.Text.Trim();
            Boolean fillable = Boolean.Parse(this.CboFillable.Text);
            String gather = this.CboGather.Text;
            this.frmEditCols.CreateColumns(colName, colTitle, colType, fillable, gather);
        }

        private void BtnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void TxtColName_Validated(object sender, EventArgs e)
        {
            if (!this.ValidateColName())
                this.TxtColName.Focus();
        }

        private Boolean ValidateColName()
        {
            if (String.IsNullOrEmpty(this.TxtColName.Text.Trim()))
            {
                MessageBox.Show("\"\"不是有效识别符");
                return false;
            }
            if (!frmEditCols.ValidateColName(this.TxtColName.Text.Trim()))
            {
                MessageBox.Show("其他列已经在使用名称 " + this.TxtColName.Text.Trim(), "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return false;
            }
            return true;
        }
    }
}
